﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;

namespace syc.Fc
{
    public partial class 套管管体日生产报表 : System.Web.UI.Page
    {
        protected HSSFWorkbook hssfworkbook;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if ((object)Session["OperatorDID"] != null)
                {
                    if ((int)Session["OperatorDID"] != 0)
                    {
                        this.FenChang.Text = ((int)Session["OperatorDID"]).ToString();
                        this.FenChang.Enabled = false;
                    }
                }
            }
            if (RiQi.Text == "")
            {
                RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                RiQi2.Text = DateTime.Today.Year.ToString() + "年" + (DateTime.Today.Month).ToString() + "月1日";
                RiQi3.Text = DateTime.Today.Year.ToString() + "年1月1日";
            }

        }

        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("报表模板/套管管体日生产报表.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "陕西延长石油材料有限公司";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "套管管体日生产报表";
            hssfworkbook.SummaryInformation = si;
        }

        protected void WriteToFile()
        {
            DateTime d;
            if (!DateTime.TryParse(this.RiQi.Text, out d))
            {
                d = DateTime.Today;
            }
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode(this.FenChang.SelectedItem.Text + "套管管体日生产报表[" + d.ToLongDateString()) + "].xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }


        protected void Button_Query_Click(object sender, EventArgs e)
        {
 
        }

        protected void Button_Export_Click(object sender, EventArgs e)
        {
            InitializeWorkbook();
            Button_Query_Click(null, null);
            HSSFSheet sheet1 = hssfworkbook.GetSheet("数据");

            HSSFRow row;
            row = sheet1.CreateRow(0);
            row.CreateCell(0).SetCellValue("日期");
            row.CreateCell(1).SetCellValue(this.RiQi.Text);

            row = sheet1.CreateRow(1);
            row.CreateCell(0).SetCellValue("生产单位");
            row.CreateCell(1).SetCellValue(this.FenChang.SelectedItem.Text);

            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            row = sheet1.CreateRow(2);
            row.CreateCell(0).SetCellValue("制表人");
            row.CreateCell(1).SetCellValue(op);



            HSSFSheet sheet2 = hssfworkbook.GetSheet("报表");
            sheet2.ForceFormulaRecalculation = true;
            WriteToFile();
            Response.End();
        }

    }
}
